package com.cxh.util.json;

import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * 时间类型转JSON辅助工具类
 */
public class JsonDateValueProcessor implements JsonValueProcessor {

		//
		/**
		 * @Fields format : jsonDate转换的默认日期格式
		 */
		private String format ="yyyy-MM-dd HH:mm:ss";

		@Override
	    public Object processArrayValue(Object value, JsonConfig config) {
	        return process(value);   
	    }
		@Override
	    public Object processObjectValue(String key, Object value, JsonConfig config) {   
	        return process(value);   
	    }   
	       
	    private Object process(Object value){   
	        if(value instanceof Date){   
	            SimpleDateFormat sdf = new SimpleDateFormat(format,Locale.UK);   
	            return sdf.format(value);   
	        }   
	        return value == null ? "" : value.toString();   
	    }

		public String getFormat() {
			return format;
		}
		public void setFormat(String format) {
			this.format = format;
		}   
}
